<template>
    <van-nav-bar
        title="订单结算"
        left-text="返回"
        left-arrow
        fixed
        @click-left="onBack"
    />

    <div style="height: 50px;"></div>

    <van-contact-card type="add" add-text="添加收获地址" @click="toAddress" v-if="!addressInfo || JSON.stringify(addressInfo) === '{}'" />

    <van-contact-card type="edit" :tel="addressInfo.mobile" :name="addressInfo.consignee" @click="toAddress" v-else />

    <van-card
        v-for="item in list"
        :key="item.id"
        :num="item.nums"
        :price="item.total"
        :desc="'商品单价：' + item.price"
        :title="item.product.name"
        :thumb="item.product.thumbs_cdn[0]"
    />

    <van-cell-group inset>
        <van-field
            v-model="content"
            rows="2"
            autosize
            label="订单备注"
            type="textarea"
            maxlength="50"
            placeholder="请输入订单备注"
            show-word-limit
            colon
            label-align='right'
            label-width="4.5em"
        />
    </van-cell-group>



    <van-submit-bar :price="total" button-text="提交订单" @submit="onSubmit" />


</template>

<script setup>
import { getCurrentInstance, ref, onMounted, reactive, computed } from 'vue';

const { proxy } = getCurrentInstance();
const cartids = ref(proxy.$route.query.cartids);
const addressInfo = ref({});
const business = reactive(proxy.$cookies.get('business'));
const list = reactive([]);
const content = ref('');

onMounted(() => {
    getCartData();

    let address = proxy.$cookies.get('address') ?? '';

    if(address)
    {
        addressInfo.value = address;
    }else{
        getAddressDefault();
    }
});

const onSubmit = async () => {

    if(business.auth == 0){
    proxy.$showNotify({
      type: "warning",
      message: "请先验证邮箱",
      duration: 2000,
    });

    return false;
  }

    let data = {
        busid:business.id,
        addrid:addressInfo.value.id,
        cartids:cartids.value,
        content:content.value
    }

    let result = await proxy.$Api.orderAdd(data);

    if(result && result.code === 1)
    {
        proxy.$showNotify({
            type:'success',
            message:'下单成功，请前往我的订单确认支付',
            duration:2000,
            onClose:() => {
                proxy.$router.push('/product/order/index');
            }
        });

        return;
    }else{
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:2000
        });

        return;
    }
}

const getCartData = async () =>
{
    let data = {
        cartids:cartids.value,
        busid:business.id
    }

    let result = await proxy.$Api.cartInfo(data);

    if(result && result.code === 1)
    {
        list.push(...result.data);
    }else{
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:2000,
            onClose:() => {
                proxy.$router.back();
            }
        });

        return;
    }
}

const total = computed(() => {
    let total = 0;

    if(list.length > 0)
    {
       for(let item of list)
       {
            total += parseFloat(item.total);
       }
    }

    return total * 100;
});

const getAddressDefault = async () =>
{
    let data = {
        busid:business.id
    }

    let result = await proxy.$Api.addressDefault(data);

    if(result && result.code === 0)
    {
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:2000
        });

        return;
    }

    addressInfo.value = result.data;
}

const toAddress = () => {
    proxy.$router.push({path:'/business/address/index',query:{action:'cart'}})
}

const onBack = () => {
    proxy.$router.back();
}
</script>